package com.weiyin.examination.util;


import android.content.Context;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.weiyin.examination.R;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;

/**
 * 计时器
 *
 * @author njf
 */
public class VerifyCodeUtils {
    public static void sendCode(final Context context, final TextView view) {
        final int count = 60;
        //设置0延迟，每隔一秒发送一条数据
        Observable.interval(0, 1, TimeUnit.SECONDS)
                //设置循环11次
                .take(count + 1)
                .map(new Func1<Long, Long>() {
                    @Override
                    public Long call(Long aLong) {
                        return count - aLong;
                    }
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //在发送数据的时候设置为不能点击
                        view.setEnabled(false);
                    }
                })
                //操作UI主要在UI线程
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        //数据发送完后设置为原来的文字
                        view.setText(context.getString(R.string.login_send));
                        view.setEnabled(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到一条就是会操作一次UI
                        view.setText(aLong + "秒");
                    }
                });

    }

    public static void sendCode(final Context context, final TextView view, final int count) {
        //设置0延迟，每隔一秒发送一条数据
        Observable.interval(0, 1, TimeUnit.SECONDS)
                //设置循环11次
                .take(count + 1)
                .map(new Func1<Long, Long>() {
                    @Override
                    public Long call(Long aLong) {
                        return count - aLong;
                    }
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //在发送数据的时候设置为不能点击
                        view.setEnabled(false);
                    }
                })
                //操作UI主要在UI线程
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        //数据发送完后设置为原来的文字
                        view.setText("0:00");
                        view.setEnabled(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到一条就是会操作一次UI
                        view.setText(format(aLong));
                    }
                });
    }


    public static void sendButtonCode(final Context context, final TextView view, final int count, Observer<Long> observer) {
        //设置0延迟，每隔一秒发送一条数据
        Observable.interval(0, 1, TimeUnit.SECONDS)
                //设置循环11次
                .take(count + 1)
                .map(new Func1<Long, Long>() {
                    @Override
                    public Long call(Long aLong) {
                        return count - aLong;
                    }
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //在发送数据的时候设置为不能点击
                        view.setEnabled(false);
                    }
                })
                //操作UI主要在UI线程
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public static void progressUpdate(final TextView view, final ProgressBar progressBar, final int sec) {
        final int count = sec;
        Observable.interval(0, 100, TimeUnit.MILLISECONDS)//设置0延迟，每隔一毫秒发送一条数据
                .take(count / 100 + 1)
                .map(new Func1<Long, Long>() {
                    @Override
                    public Long call(Long aLong) {
                        return count - aLong * 100; //
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//操作UI主要在UI线程
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到一条就是会操作一次UI
                        LogUtil.d("VerifyCodeUtils", "aLong = " + aLong + "");
                        int progress = (int) ((1 - (aLong.doubleValue() / sec)) * 100);
                        LogUtil.d("VerifyCodeUtils", "progress = " + progress + "");
                        view.setText(format(aLong));
                        progressBar.setProgress(progress);
                    }
                });
    }

    private static String format(long timeLengt) {
        long m = timeLengt / 3600;
        long s = timeLengt % 60;
        timeLengt = (timeLengt - (m * 3600)) / 60;
        String min = String.valueOf(timeLengt);
        String small = String.valueOf(s);
        String hour = String.valueOf(m);
        if (m < 10) {
            hour = "0" + hour;
        }
        if (s < 10) {
            small = "0" + small;
        }
        if (timeLengt < 10) {
            min = "0" + min;
        }
        return hour + ":" + min + ":" + small;
    }
}
